package com.xxxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xxxy.fallback.SentinelFallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sentinel")
public class SentinelController {

    @GetMapping("/rt")
    @SentinelResource(value = "getRtResource", fallback = "rtFallback",
             fallbackClass = SentinelController.class)
    public String getRt() {
        try {
            Thread.sleep(2000);
        }catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "进入 Rt";
    }
    @GetMapping("/exception")
    @SentinelResource(value = "exceptionTest",
            fallback = "rtFallback",
            fallbackClass = SentinelFallback.class)
    public String testException() {
        // 模拟异常情况
        if (System.currentTimeMillis() % 2 == 0) {
            throw new RuntimeException("测试异常");
        }
        return "正常返回";
    }
}
